#include "com_voe_VoEHardware.h"
#include "jni_api.h"
#include "OutputParam.h"
#include "voe_hardware.h"

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetInterface
 * Signature: (J)J
 */
JNIEXPORT jlong JNICALL Java_com_voe_VoEHardware_GetInterface
	(JNIEnv* env, jclass cls, jlong p_voiceEngine)
{
	return (jlong)webrtc::VoEHardware::GetInterface((webrtc::VoiceEngine*)p_voiceEngine);
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    Release
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_Release
	(JNIEnv* env, jclass cls, jlong p)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->Release();
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetNumOfRecordingDevices
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_GetNumOfRecordingDevices
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	int devices = 0;
	jint result = pVoEHardware->GetNumOfRecordingDevices(devices);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(devices).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetNumOfPlayoutDevices
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_GetNumOfPlayoutDevices
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	int devices = 0;
	jint result = pVoEHardware->GetNumOfPlayoutDevices(devices);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(devices).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetRecordingDeviceName
 * Signature: (JILcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_GetRecordingDeviceName
	(JNIEnv* env, jclass cls, jlong p, jint index, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	char strNameUTF8[128], strGuidUTF8[128];
	jint result = pVoEHardware->GetRecordingDeviceName(index, strNameUTF8, strGuidUTF8);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(std::string(strNameUTF8)).add(std::string(strGuidUTF8)).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetPlayoutDeviceName
 * Signature: (JILcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_GetPlayoutDeviceName
	(JNIEnv* env, jclass cls, jlong p, jint index, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	char strNameUTF8[128], strGuidUTF8[128];
	jint result = pVoEHardware->GetPlayoutDeviceName(index, strNameUTF8, strGuidUTF8);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(std::string(strNameUTF8)).add(std::string(strGuidUTF8)).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetRecordingDeviceStatus
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_GetRecordingDeviceStatus
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	bool isAvailable;
	jint result = pVoEHardware->GetRecordingDeviceStatus(isAvailable);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(isAvailable).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetPlayoutDeviceStatus
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_GetPlayoutDeviceStatus
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	bool isAvailable;
	jint result = pVoEHardware->GetPlayoutDeviceStatus(isAvailable);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(isAvailable).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    SetRecordingDevice
 * Signature: (JII)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_SetRecordingDevice
  (JNIEnv* env, jclass cls, jlong p, jint index, jint recordingChannel)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->SetRecordingDevice(index, (webrtc::StereoChannel)recordingChannel);
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    SetPlayoutDevice
 * Signature: (JI)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_SetPlayoutDevice
	(JNIEnv* env, jclass cls, jlong p, jint index)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->SetPlayoutDevice(index);
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    SetAudioDeviceLayer
 * Signature: (JI)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_SetAudioDeviceLayer
  (JNIEnv* env, jclass cls, jlong p, jint audioLayer)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->SetAudioDeviceLayer((webrtc::AudioLayers)audioLayer);
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetAudioDeviceLayer
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_GetAudioDeviceLayer
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	webrtc::AudioLayers audioLayer;
	jint result = pVoEHardware->GetAudioDeviceLayer(audioLayer);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add((int)audioLayer).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetCPULoad
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_GetCPULoad
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	int loadPercent;
	jint result = pVoEHardware->GetCPULoad(loadPercent);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(loadPercent).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetSystemCPULoad
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_GetSystemCPULoad
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	int loadPercent;
	jint result = pVoEHardware->GetSystemCPULoad(loadPercent);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(loadPercent).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    ResetAudioDevice
 * Signature: (J)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_ResetAudioDevice
	(JNIEnv* env, jclass cls, jlong p)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->ResetAudioDevice();
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    AudioDeviceControl
 * Signature: (JIII)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_AudioDeviceControl
	(JNIEnv* env, jclass cls, jlong p, jint par1, jint par2, jint par3)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->AudioDeviceControl(par1, par2, par3);
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    SetLoudspeakerStatus
 * Signature: (JZ)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_SetLoudspeakerStatus
	(JNIEnv* env, jclass cls, jlong p, jboolean enable)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->SetLoudspeakerStatus(enable != 0);
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    GetLoudspeakerStatus
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_GetLoudspeakerStatus
  (JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	bool enabled;
	jint result = pVoEHardware->GetLoudspeakerStatus(enabled);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add(enabled).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    SetRecordingSampleRate
 * Signature: (JI)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_SetRecordingSampleRate
	(JNIEnv* env, jclass cls, jlong p, jint samples_per_sec)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->SetRecordingSampleRate(samples_per_sec);
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    RecordingSampleRate
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_RecordingSampleRate
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	unsigned int samples_per_sec;
	jint result = pVoEHardware->RecordingSampleRate(&samples_per_sec);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add((int)samples_per_sec).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    SetPlayoutSampleRate
 * Signature: (JI)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_SetPlayoutSampleRate
	(JNIEnv* env, jclass cls, jlong p, jint samples_per_sec)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->SetPlayoutSampleRate(samples_per_sec);
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    PlayoutSampleRate
 * Signature: (JLcom/voe/common/OutputParam;)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_PlayoutSampleRate
	(JNIEnv* env, jclass cls, jlong p, jobject obj)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	unsigned int samples_per_sec;
	jint result = pVoEHardware->PlayoutSampleRate(&samples_per_sec);
	if(result == 0)
	{
		JniOutputBuilder job(env);
		OutputParam param(env, obj);
		param.setArray(job.add((int)samples_per_sec).getOutput());
	}
	return result;
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    EnableBuiltInAEC
 * Signature: (JZ)I
 */
JNIEXPORT jint JNICALL Java_com_voe_VoEHardware_EnableBuiltInAEC
	(JNIEnv* env, jclass cls, jlong p, jboolean enable)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->EnableBuiltInAEC(enable!=0);
}

/*
 * Class:     com_voe_VoEHardware
 * Method:    BuiltInAECIsEnabled
 * Signature: (J)Z
 */
JNIEXPORT jboolean JNICALL Java_com_voe_VoEHardware_BuiltInAECIsEnabled
  (JNIEnv* env, jclass cls, jlong p)
{
	webrtc::VoEHardware* pVoEHardware = (webrtc::VoEHardware*)p;
	return pVoEHardware->BuiltInAECIsEnabled();
}
